﻿#include <string.h>

#include <stdio.h>
#include <stdlib.h>

//#define WIN32_LEAN_AND_MEAN
#include "webui.h"
#include <windows.h>
#include "wechat.h"
#include "win.h"

enum app_state gstate = APP_STATE_UNINIT;

/*
 fetch_video_url(current_idx)；
 response = new_idx, title, size, seed
*/
static char response_js_str[4096];
void fetch_video_url(webui_event_t* e)
{
	struct video_info *pvideo;
	long long idx = webui_get_int_at(e, 0);

	if (current_video_index > MAX_VIDEO_NUM) {
		sprintf(response_js_str, "%lld, video number is more than 32", idx);
		webui_return_string(e, response_js_str);
		return;
	}

	if (idx >= current_video_index) {
		//keep original
		idx = current_video_index;
		sprintf(response_js_str, "%lld", idx);
		webui_return_string(e, response_js_str);
		return;
	}

	if (gstate == APP_STATE_DOWNLOADING) {
		//keep original
		sprintf(response_js_str, "%lld, downloading, 0, 0", idx);
		webui_return_string(e, response_js_str);
		return;
	}
	
	//fetch next video
	printf("fetch new one \n");
	idx++;
	pvideo = &video_info[idx - 1];
	char* utf_str;
	int out_len;
	utf_str = to_utf(pvideo->title, &out_len);
	sprintf(response_js_str, "%lld, %s, %llu, %lld", idx, utf_str, pvideo->size, pvideo->seed);
	free(utf_str);
	printf("fetch idx=%lld, current_video_index = %d\n", idx, current_video_index);
	webui_return_string(e, response_js_str);
}

void reset_videos(char* log)
{
	current_video_index = 0;
	memset(video_info, 0, sizeof(struct video_info) * MAX_VIDEO_NUM);
	printf("clear all video itmes\n");
}

//download video
int start_download(long long id, char* log)
{
	if (id >= (current_video_index + 1)) {
		printf("no this video item id = %lld\n", id);
		return 1;
	}

	struct video_info *pvideo = &video_info[id - 1];
	if (pvideo->state == VIDEO_DOWNLAODED) {
		printf("already downloaded video: %s\n\n", pvideo->title);
		return 0;
	}

	gstate = APP_STATE_DOWNLOADING;

	printf("ongoing downloading video: id:%lld, title:%s\n", id, pvideo->title);
	download_video(pvideo);
	decode_video_with_seed(pvideo->seed, "enc.mp4", "dec.mp4");
	moveto_download_dir(pvideo->title);
	pvideo->state = VIDEO_DOWNLAODED;
	printf("OK to download video: %s\n\n", pvideo->title);
	gstate = APP_STATE_RUN;
	return 0;
}

/*
	flag = 100: delete cache
	flag = 200: clear all video items
	flag = 300: open download dir
*/
void action_run(webui_event_t* e)
{
	long long flag = webui_get_int_at(e, 0);

	strcpy(response_js_str, "OK");
	if (flag == 100) { 
		//delete cache
		delete_cache(response_js_str);
	} else if (flag == 200) {
		//clear all video items
		reset_videos(response_js_str);
	} else if (flag == 300) {
		//open download dir
		ShellExecute(
			NULL,         
			L"open",
			L".\\videos",
			NULL,
			NULL,
			SW_SHOWNORMAL
		);
	} else {
		//download video
		int ret;
		ret = start_download(flag, response_js_str);
		if (ret) {
			strcpy(response_js_str, "failed");
		} else {
			strcpy(response_js_str, "OK");
		}
	}

	webui_return_string(e, response_js_str);
}

void webui_init(void)
{
	// Create a new window
	size_t MainWindow = webui_new_window();

	// Set the root folder for the UI
	//webui_set_root_folder(MainWindow, ".");

	// Bind HTML elements with the specified ID to C functions
	webui_bind(MainWindow, "fetch_video_url", fetch_video_url);
	webui_bind(MainWindow, "action_run", action_run);

	// Show the window, preferably in a chromium based browser
	if (!webui_show_browser(MainWindow, "GUI.html", AnyBrowser))
		webui_show(MainWindow, "GUI.html");


	// Wait until all windows get closed
	webui_wait();

	// Free all memory resources (Optional)
	webui_clean();
}

int main(int argc, char** argv)
{
	mitm_init();
	if (mitm_start())
		return -1;
	//start UI
	webui_init();
	//exit
	mitm_exit();
	return 0;
}
